package com.jinkaimen.goodcash.ui.activity;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.ht.baselibs.bean.SelectItemBean;
import com.ht.baselibs.ui.adapter.SelectItemAdapter;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.ui.widget.TitleBarView;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.SharedPreferencesUtils;
import com.jinkaimen.goodcash.constant.ConstantPool;
import com.jinkaimen.goodcash.utils.VoiceUtils;

import java.util.ArrayList;

public class NotificationSettingActivity extends BaseActivity {
    private static ArrayList<String> mNotificationList;

    static {
        mNotificationList = new ArrayList<>();
        mNotificationList.add("报金额");
        mNotificationList.add("收银声");
        mNotificationList.add("不提示");
    }

    private ArrayList<SelectItemBean> mDataList;
    private SelectItemAdapter mAdapter;
    private ListView mListView;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("收款提示音").addActions(new TitleBarView.Action("保存") {
            @Override
            public void performAction(View view) {
                ActivityUtils.finishActivity(NotificationSettingActivity.this);
            }
        });
    }

    @Override
    protected int initContentView() {
        return com.ht.baselibs.R.layout.activity_select_list;
    }

    @Override
    protected void initChildView() {
        mListView = (ListView) findViewById(com.ht.baselibs.R.id.lv_activity_select_list);
    }

    @Override
    protected void initChildData() {
        int notificationType = SharedPreferencesUtils.getInt(ConstantPool.NOTIFICATION_TYPE, 0);
        mDataList = new ArrayList<>();
        int size = mNotificationList.size();
        for (int i = 0; i < size; i++) {
            String title = mNotificationList.get(i);
            if (notificationType == i){
                mDataList.add(new SelectItemBean(title, true));
            }else{
                mDataList.add(new SelectItemBean(title));
            }
        }

        mAdapter = new SelectItemAdapter(this, mDataList);
        mListView.setAdapter(mAdapter);
    }

    @Override
    protected void setChildViewListener() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                for (int i = 0; i < mDataList.size(); i++) {
                    SelectItemBean selectItemBean = mDataList.get(i);
                    selectItemBean.isSelect = false;
                }

                SelectItemBean selectItemBean = mDataList.get(position);
                selectItemBean.isSelect = true;
                mAdapter.notifyDataSetChanged();

                SharedPreferencesUtils.put(ConstantPool.NOTIFICATION_TYPE, position);

                VoiceUtils.stop();
                VoiceUtils.stopDetails();

                if (position == 0){
                    VoiceUtils.playDetails("微信收款3.82元，收款码3127");
                } else if (position == 1){
                    VoiceUtils.play();
                } else {
//                    UIUtils.showToast("无反应");
                }

            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        VoiceUtils.stopDetails();
        VoiceUtils.stop();
    }
}
